import './design/index.less';
import { createApp } from 'vue';
import { router } from './router';
import App from './App.vue';
import { chain, configureChains, createClient, VagmiPlugin } from 'vagmi';
import { publicProvider } from 'vagmi/providers/public';
import { infuraProvider } from 'vagmi/providers/infura';
import { InjectedConnector } from 'vagmi/connectors/injected';
import { monitor } from './utils/monitor';
import { setupStore } from './store';
import { setupRouterGuard } from '/@/router/guard';
import useTimeAgo from '/@/hooks/useTimeAgo';

const { chains, provider, webSocketProvider } = configureChains(
  [import.meta.env.PROD ? chain.polygon : chain.polygonMumbai],
  [infuraProvider({ infuraId: import.meta.env.VITE_INFURA_API_KEY }), publicProvider()],
);
const client = createClient({
  autoConnect: true,
  connectors: [new InjectedConnector({ chains })],
  provider,
  webSocketProvider,
});

const app = createApp(App);

// 状态管理
setupStore(app);

app.use(router);
app.use(VagmiPlugin(client));
app.use(useTimeAgo);

// router-guard
// 路由守卫
setupRouterGuard(router);

// 错误处理
app.config.errorHandler = (error: Error) => {
  monitor.track(error);
};

app.mount('#app');
